<!-- 建筑树 -->
 <template>
    <div class="buildTree">
        <!-- <table-index
            :dataSource="dataSource"
            :tableColumns="tableColumns"
            :btnList="[]"
            @handleCurrentChange="handleCurrentChange"
            :showPage="false"
            :height="height"
            rowKey="id"
        ></table-index> -->

        <div
            class="buildTreeHome"
            :style="{
                height: height + 'px'
            }"
        >
            <!-- <Tree :data="dataSource" :render="renderContent" @on-select-change="handleCurrentChange"></Tree> -->
            <Tree
                :data="dataSource"
                :load-data="handleLoadData"
                @on-select-change="handleCurrentChange"
                :check-strictly="checkStrictly ? true : false"
                :show-checkbox="showCheck ? true : false"
                @on-check-change="checkChange"
            ></Tree>
        </div>
    </div>
</template>
 
 <script>
import tableIndex from '@/components/customComponents/customeTable/iviewTable/singleViewTableBuildAsync'
import {AjaxCommon} from '@/libs/apiCommon'
import {PopupCommon} from '@/libs/popupCommon'
export default {
    /** eqtype: ZMKZQ 照明插座,LDKZQ 路灯,KT 空调 */
    props: ['height', 'name', 'eqtype', 'showCheck', 'checkStrictly'],
    name: 'buildTree',
    components: {tableIndex},
    data() {
        return {
            dataSource: [],
            // tableColumns: [
            //     {
            //         key: 'text',
            //         title: '建筑名称',
            //         tree: true
            //     }
            // ]
            firstItem: {}
        }
    },
    computed: {},
    created() {
        this.getBuildData()
        // if (this.name) {
        //     this.tableColumns[0].title = this.name
        // }
    },
    methods: {
        checkChange(list) {
            this.$emit('onCheck', list)
        },
        handleLoadData(item, callback) {
            let eqtype = 'all'
            if (this.eqtype) {
                eqtype = this.eqtype
            }
            const depth = parseInt(item.attributes)
            const params = `/api/Build/GetBuildTreeRT/${depth}/${eqtype}/${item.id}`
            AjaxCommon(params, 'get', null).then((res) => {
                if (res?.data?.Code == 200) {
                    const data = res?.data.Message
                    data?.map((t) => {
                        t.text = t.title
                        if (t.state == 'closed') {
                            t._loading = false
                            t.loading = false
                            t.children = []
                        }
                        t.render = (h, {root, node, data}) => {
                            return h('span', [
                                h('Icon', {
                                    props: {
                                        type: 'icon-' + t.type
                                    },
                                    style: {
                                        marginRight: '8px'
                                    }
                                }),
                                h('span', data.title)
                            ])
                        }
                        return t
                    })
                    this.$emit('load', JSON.stringify(data))
                    callback(data)
                } else {
                    PopupCommon.notify('error', res?.data?.msg)
                }
            })
        },
        // renderContent(h, {root, node, data}) {
        //     return h('span', [
        //         h('Icon', {
        //             props: {
        //                 type: 'icon-' + data.attributes.type
        //             },
        //             style: {
        //                 marginRight: '8px'
        //             }
        //         }),
        //         h('span', data.text)
        //     ])
        // },
        getBuildData() {
            let self = this
            let eqtype = 'all'
            if (this.eqtype) {
                eqtype = this.eqtype
            }
            AjaxCommon(`/api/Build/GetBuildTreeRT/-1/${eqtype}/`, 'get', null).then((res) => {
                if (res?.data?.Code == 200) {
                    const data = res?.data.Message
                    data?.map((t, i) => {
                        t.text = t.title
                        if (t.state == 'closed') {
                            t._loading = false
                            t.loading = false
                            t.children = []
                        }
                        if (i == 0) {
                            self.firstItem = t
                        }
                        t.render = (h, {root, node, data}) => {
                            return h('span', [
                                h('Icon', {
                                    props: {
                                        type: 'icon-' + data.type
                                    },
                                    style: {
                                        marginRight: '8px'
                                    }
                                }),
                                h('span', data.text)
                            ])
                        }
                        return t
                    })
                    self.dataSource = data
                    self.$emit('allData', JSON.stringify(data))
                    setTimeout(() => {
                        try {
                            document.getElementsByClassName('ivu-tree')[0].children[0].children[0].children[0].click()
                        } catch (error) {}
                    }, 100)
                } else {
                    PopupCommon.notify('error', res?.data?.msg)
                }
                self.$emit('dataLoad', '')
            })
        },
        handleCurrentChange(item) {
            // this.$emit('handleCurrentChange', item)

            this.$emit('handleCurrentChange', item[0])
        }
    }
}
</script>
<style lang="scss">
.buildTree {
    .buildTreeHome {
        overflow: auto;
        &::-webkit-scrollbar {
            width: 0px; // 滚动条宽度
            display: none;
        }
        font-size: 14px;
        span {
            font-size: 14px;
        }
    }

    .ivu-icon-icon-bd {
        position: relative;
        height: 14px;
        width: 14px;
        &::before {
            position: absolute;
            top: 0;
            left: 0;
            height: 14px;
            width: 14px;
            content: '';
            background: url('@/assets/images/other/buildings.png') no-repeat;
            background-size: 100%;
        }
    }
    .ivu-icon-icon-eq {
        position: relative;
        height: 14px;
        width: 14px;
        &::before {
            position: absolute;
            top: 0;
            left: 0;
            height: 14px;
            width: 14px;
            content: '';
            background: url('@/assets/images/other/deviceGray.png') no-repeat;
            background-size: 100%;
        }
    }
}
</style>
